package class5;

import common.TreeNode;
import common.TreeNodeUtils;

/**
 * 二叉树
 * +     1
 * +    / \
 * +   2   3
 * 1.二叉树不能有环路
 * <p>
 * 树的前序遍历、中序遍历、后序号遍历
 */
public class Code01_BinaryTree {

    public static void main(String[] args) {
        TreeNode treeNode1 = TreeNodeUtils.createTreeNode(1, 2, 3);
        TreeNode left = treeNode1.left;
        TreeNode right = treeNode1.right;
        TreeNode[] t = TreeNodeUtils.getTreeNodes(4, 5, 6, 7);
        left.left = t[0];
        left.right = t[1];
        right.left = t[2];
        right.right = t[3];

        System.out.print("先序：");
        pre(treeNode1);

        System.out.println();
        System.out.print("中序：");
        mid(treeNode1);

        System.out.println();
        System.out.print("后序：");
        pos(treeNode1);
    }

    public static void f(TreeNode cur) {
        if (cur == null) return;
        f(cur.left);
        f(cur.right);
    }

    // 1.前序遍历
    public static void pre(TreeNode cur) {
        if (cur == null) return;
        System.out.print(cur.val + "\t");
        pre(cur.left);
        pre(cur.right);
    }

    // 2.中序遍历
    public static void mid(TreeNode cur) {
        if (cur == null) return;
        mid(cur.left);
        System.out.print(cur.val + "\t");
        mid(cur.right);
    }

    // 3.后序遍历
    public static void pos(TreeNode cur) {
        if (cur == null) return;
        pos(cur.left);
        pos(cur.right);
        System.out.print(cur.val + "\t");

    }
}
